﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.TCenter
{
    public partial class TCenterAnnexe : System.Web.UI.Page
    {
        private int userId;
        private int cUserId;
        private int venuesId;        
        protected string ModuleId;
        private Jy123.Model.TCenter_Main TCenter;
        private Jy123.Model.Module_Main module;
        private Jy123.Model.Public_Level level;
        private Jy123.Model.User_Main user;

        private string[] strContent = { "我的中心", "中心升级", "训练设备", "组件排行", "训练信息", "组件维护", "商业活动" };
        private string[] strCss = { "lmenu2", "lmenu2", "lmenu3", "lmenu2", "lmenu2", "lmenu2", "lmenu2" };
        private string[] strHeaderCss = { "", "", "NavIcon", "" };


        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            if (!Page.IsPostBack)
            {
                cUserId = int.Parse(strUserIdFromCookie);
                LoadInitialValue();
            }
        }

        private void LoadInitialValue()
        {
            string key = "TCenterId";
            string venuesIdString = Request.QueryString[key];
            if (!string.IsNullOrEmpty(venuesIdString))
            {
                venuesId = int.Parse(venuesIdString);
                ModuleId = venuesIdString;
                Jy123.BLL.TCenter_Main BLLVenues = new Jy123.BLL.TCenter_Main();
                Jy123.BLL.Module_Main BLLModuleMain = new Jy123.BLL.Module_Main();
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

                TCenter = BLLVenues.GetModel(venuesId);
                userId = TCenter.FUser.Value;
                user = BLLUser.GetModel(userId);
                level = BLLLevel.GetModel(TCenter.FLevel.Value);
                module = BLLModuleMain.GetModel(venuesId);                
                BindHTML();
            }
            else
            {
                //找不到UserId，返回首页,目前暂时返回空值
            }
        }

        private void BindHTML()
        {
            string strHTML = Cache["annexelist"].ToString();
            Jy123.Web.AppCode.Module appModule = new Jy123.Web.AppCode.Module();
            string strHeader = Cache["header"].ToString();
            string headerString = appModule.BindHeader(strHeader, strHeaderCss);
            string strLeft = Cache["leftmenu"].ToString();
            string strLeftLi = Cache["leftmenuli"].ToString();
            string strTail = "?TCenterId=" + TCenter.ID.ToString();
            string[] strHref = { "MyTCenter.aspx" + strTail, "UpdateTCenter.aspx" + strTail,"#",  "TCenterRank.aspx" + strTail, "TrainInfo.aspx" + strTail, "TCenterRepair.aspx" + strTail, "TCenterHire.aspx" + strTail };
            string[] strNoRaceHref = { "MyTCenter.aspx" + strTail, "UpdateTCenter.aspx" + strTail,"#",  "TCenterRank.aspx" + strTail, "javascript:$.moonbox.warning('您的训练中心没有进行俱乐部进行训练！')", "TCenterRepair.aspx" + strTail, "TCenterHire.aspx" + strTail };
            string leftMenuString;
            if (TCenter.CurrentClubs.Value > 0)
            {
                leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strHref, strCss);
            }
            else
            {
                leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strNoRaceHref, strCss);
            }
            string strClub = Cache["contentclub"].ToString();
            string contentString = appModule.BindContentClub(strClub, module, TCenter, user, level);
            string strMain = Cache["mainmenu"].ToString();
            string strDiv = Cache["mainmenusubdiv"].ToString();
            string strLi = Cache["mainmenusubdivli"].ToString();
            string mainMenuString = appModule.BindMainMenu(strMain, strDiv, strLi, cUserId);
            strHTML = strHTML.Replace("$Header$", headerString);
            strHTML = strHTML.Replace("$MainMenu$", mainMenuString);
            if (cUserId == userId)
            {
                strHTML = strHTML.Replace("$LeftMenu$", leftMenuString);
            }
            else
            {
                strHTML = strHTML.Replace("$LeftMenu$", string.Empty);
            }
            strHTML = strHTML.Replace("$ContentClub$", contentString);
            strHTML = strHTML.Replace("$Footer$", Cache["footer"].ToString());
            strHTML = strHTML.Replace("$ModuleId$", TCenter.ID.ToString());
            strHTML = strHTML.Replace("$VenuesName$", TCenter.TName);

            BLL.Public_AnnexeType BLLAType = new Jy123.BLL.Public_AnnexeType();
            List<Model.Public_AnnexeType> listPType = BLLAType.GetModelList("ID = 9");
            StringBuilder sbPType = new StringBuilder();
            StringBuilder sbPSType = new StringBuilder();
            foreach (Model.Public_AnnexeType aType in listPType)
            {
                string strPLi = Cache["annexeli"].ToString();
                string strPSLi = Cache["annexeli"].ToString();
                string strClick = "$(function(){fillhtml('listUl','/Module/Venues/AnnexeList.aspx?Type=Annexe&PType=" + aType.ID.ToString() + "&VenuesId=" + TCenter.ID.ToString() + "&random=Math.random()'),$('#selTypeHidden').val(" + aType.ID.ToString() + ")})";
                string strSClick = "$(function(){fillhtml('listUl','/Module/Venues/AnnexeList.aspx?Type=AnnexeStore&PType=" + aType.ID.ToString() + "&VenuesId=" + TCenter.ID.ToString() + "&random=Math.random()'),$('#selTypeHidden').val(" + aType.ID.ToString() + ")})";
                strPLi = strPLi.Replace("$LiId$", aType.ID.ToString());
                strPLi = strPLi.Replace("$ClickEvent$", strClick);
                strPLi = strPLi.Replace("$LiContent$", aType.AName);
                strPSLi = strPSLi.Replace("$LiId$", aType.ID.ToString());
                strPSLi = strPSLi.Replace("$ClickEvent$", strSClick);
                strPSLi = strPSLi.Replace("$LiContent$", aType.AName);
                sbPType.Append(strPLi);
                sbPSType.Append(strPSLi);
            }
            strHTML = strHTML.Replace("$Li1$", sbPType.ToString());
            strHTML = strHTML.Replace("$Li2$", sbPSType.ToString());
            strHTML = strHTML.Replace("$PTypeId$", listPType[0].ID.ToString());
            Response.Write(strHTML);
        }
    }
}
